﻿using CourseManagerment.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CourseManagerment.ViewModel
{
    public class CoursePageViewModel
    {
        /// <summary>
        /// 课程分类
        /// </summary>
        public ObservableCollection<CategoryItemModel> CategoryCourse { get; set; }
        /// <summary>
        /// 技术分类
        /// </summary>
        public ObservableCollection<CategoryItemModel> CategoryTechnology { get; set; }
        /// <summary>
        /// 授课老师
        /// </summary>
        public ObservableCollection<CategoryItemModel> CategoryTeacher { get; set; }
        /// <summary>
        /// 课程信息
        /// </summary>
        public ObservableCollection<CourseModel> CourseList { get; set; } = new();
        /// <summary>
        /// 所有课程信息
        /// </summary>
        public List<CourseModel> CourseAll { get; set; }
        /// <summary>
        /// 打开课程连接命令
        /// </summary>
        public CommandBase OpenCourseUrlCommand { get; set; } = new() { DoCanExecute=e=>true};
        public CommandBase TeacherFilterCommand { get; set; } = new() { DoCanExecute = e => true };
        public CoursePageViewModel()
        {
            this.OpenCourseUrlCommand.DoExecute = o=> System.Diagnostics.Process.Start(o.ToString());//打开网址

            this.TeacherFilterCommand.DoExecute = DoFilter;

            this.InitCategory();
            this.InitCourseList();
        }
        /// <summary>
        /// 根据老师过滤课程信息
        /// </summary>
        /// <param name="o"></param>
        private void DoFilter(object o)
        {
            string teacher = o.ToString();
            List<CourseModel> temp = CourseAll;
            if (teacher != "全部")
                temp = CourseAll.Where(c => c.Teachers.Exists(e => e == teacher)).ToList();

            CourseList.Clear();
            foreach (var item in temp)
                CourseList.Add(item);
        }
        private void InitCategory()
        {
            this.CategoryCourse = new ObservableCollection<CategoryItemModel>();
            this.CategoryCourse.Add(new CategoryItemModel("全部", true));
            this.CategoryCourse.Add(new CategoryItemModel("公开课"));
            this.CategoryCourse.Add(new CategoryItemModel("VIP课"));

            this.CategoryTechnology = new ObservableCollection<CategoryItemModel>();
            this.CategoryTechnology.Add(new CategoryItemModel("全部", true));
            this.CategoryTechnology.Add(new CategoryItemModel("C#"));
            this.CategoryTechnology.Add(new CategoryItemModel("ASP.NET"));
            this.CategoryTechnology.Add(new CategoryItemModel("Java"));

            this.CategoryTeacher = new ObservableCollection<CategoryItemModel>();
            this.CategoryTeacher.Add(new CategoryItemModel("全部", true));
#if !DEBUG
            foreach (var item in LocalDataAccess.GetInstance().GetTeachers())
                this.CategoryTeacher.Add(new CategoryItemModel(item));
#endif
        }
        private void InitCourseList()
        {
            //设置显示骨架屏
            for (int i = 0; i < 10; i++)
            {
                CourseList.Add(new CourseModel { IsShowSkeleton = true });
            }

#if !DEBUG
            //费时操作
            Task.Run(new Action(async () =>
            {
                this.CourseAll = LocalDataAccess.GetInstance().GetCourses();
                await Task.Delay(4000);//等待4秒

                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    CourseList.Clear();
                    foreach (var item in CourseAll)
                        this.CourseList.Add(item);
                }));

            }));
#endif
        }
    }
}
